import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { withRouter } from 'react-router-dom';
import {Tools,Toast, TimeRangePicker,InputInterval, List, Switch,Checkbox,Button,WhiteSpace,Select } from 'weaver-mobile';
import { Browser,SelectPage,BrowserHrm} from 'weaver-mobile-page';
const CheckboxItem = Checkbox.CheckboxItem;
const getLabel = Tools.getLabel;
@inject('addShareStore','userCenterStore')
@withRouter
@observer
export default class AddShare extends Component {
    constructor(props) {
        super(props);
        this.state = {
          valueArray: ['1'],
          type:'1',
          date: [],
          en: false,
          roleLevel:'2'
        };
         this.options = [
       
            {
              key: '1',
              showname: getLabel('179','人力资源'),
            },
            {
              key: '2',
              showname: getLabel('33553','分部'),
            },
            {
              key: '3',
              showname: getLabel('27511','部门'),
            },
            {
              key: '4',
              showname: getLabel('122','角色'),
            },
            {
              key: '5',
              showname: getLabel('1340','所有人'),
            }
          ];
           this.options2 = [
            {
              key: '2',
              showname: getLabel('140','总部') ,
            },
            {
              key: '1',
              showname: getLabel('33553','分部'),
            },
            {
              key: '0',
              showname: getLabel('27511','部门') ,
            }
          ];
          this.shareValue="";
          this.shareValuespan="";
          this.canViewMinTime="";
          this.canViewMaxTime="";
          this.seclevel="10";
          this.secLevelMax="100";
      }
   
      addShare= (setState)=>{
        const { userCenterStore: { userShareSetStore } } = this.props;
        const { myShareList } = userShareSetStore;
        let valueArray = this.state.valueArray
         let params = {};
         if(this.shareValue=="" && valueArray!="5"){
          Toast.fail(getLabel('385600','对象不能为空'), 1);
          return;
         }
         params["shareValue"] = this.shareValue;
         if(valueArray!="1"){
            params["secLevel"] =  this.seclevel;
            params["secLevelMax"] = this.secLevelMax;
            if(this.seclevel==""||this.secLevelMax==""){
                Toast.fail(getLabel('23251','安全级别不能为空') , 1);
                return;
            }
         }
         if(valueArray=="2"){
          this.getContainLower(params);
        }else if(valueArray=="3"){
          this.getContainLower(params);
        }else if(valueArray=="4"){
            params["roleLevel"] = this.state.roleLevel;
        }else if(valueArray=="5"){
            params["shareValue"] = "";
        }
        params["canViewMinTime"] =  this.canViewMinTime;
        params["canViewMaxTime"] =  this.canViewMaxTime;
        params["shareValuespan"] = this.shareValuespan;
        params["shareType"] = valueArray[0];
        params["operation"] = "saveShare";
        const options = {
            url: '/api/blog/shareset/operateBlogShareSet',
            method: 'POST',
            params: params
          };
          Tools.callApi(options).then((src) => {
            if (src.status=="1") {
                myShareList.clearData();
                setState({ browserSubValue: [],browserDeptValue:[],browserRoleValue:[],browserHrmValue:[]});
                this.props.history.push({ pathname : '/userCenter/setting/shareSet'})
            }
          });
      }


  getContainLower(params){
    if(this.state.en){
      params["containLower"]  = "1"
    }else{
      params["containLower"]  = "0"
    }
  }  
  onChange(datas,setState){
    const {browserSub,browserDept,browserRole,browserHrm} = this.props.addShareStore;
    let type = "1";
    if(datas[0]["key"]=="1"){
        type = "1";
    }else if(datas[0]["key"]=="2"){
        this.setState({en:false})
        type = "164";
    }else if(datas[0]["key"]=="3"){
        this.setState({en:false})
        type = "4";
    }else if(datas[0]["key"]=="4"){
        type = "267";
    }else if(datas[0]["key"]=="5"){
        type = "0";
    }
    //164,4,267
    browserSub.clear();
    browserDept.clear();
    browserRole.clear();
    setState({ browserSubValue: [],browserDeptValue:[],browserRoleValue:[]});
    this.setState({type:type,valueArray:[datas[0]["key"]]})
  } 

  changeLanguage = () => {
    this.setState({
      en: !this.state.en,
    });
  }
  render() {
    const { browser, setState,selectPage,browserHrmValue,browserHrm,browserValue,browserSub,browserDept,browserRole,browserSubValue,browserDeptValue,browserRoleValue } = this.props.addShareStore;
    return (
      <div style={{height:'100%'}}>
        <SelectPage
          options={this.options}
          store={selectPage}
          label={getLabel('500401','类型') }
          viewAttr={3}
          valueArray={this.state.valueArray}
          onChange={datas => this.onChange(datas,setState)}
        />
        {this.state.type=='1'?
        <BrowserHrm
        label={getLabel('106','对象') }
        store={browserHrm}
        hasAdvanceSerach
        isSingle={false}
        type={this.state.type}
        onChange={(value, valueSpan, valueArray) => {
          this.shareValue = value;
          this.shareValuespan = valueSpan;
          setState({ browserHrmValue: valueArray });
        }}
        valueArray={browserHrmValue}
      />:(this.state.type=='164'?
        <Browser
        label={getLabel('106','对象') }
        store={browserSub}
        hasAdvanceSerach
        isSingle={false}
        type={this.state.type}
        onChange={(value, valueSpan, valueArray) => {
            this.shareValue = value;
            this.shareValuespan = valueSpan;
            setState({ browserSubValue: valueArray});
        }}
        valueArray={browserSubValue}
        />:(this.state.type=='4'?
        <Browser
        label={getLabel('106','对象') }
        store={browserDept}
        hasAdvanceSerach
        isSingle={false}
        type={this.state.type}
        onChange={(value, valueSpan, valueArray) => {
            this.shareValue = value;
            this.shareValuespan = valueSpan;
            setState({ browserDeptValue: valueArray});
        }}
        valueArray={browserDeptValue}
        />:(this.state.type=='267'?
        <Browser
        label={getLabel('106','对象') }
        store={browserRole}
        hasAdvanceSerach
        isSingle={false}
        type={this.state.type}
        onChange={(value, valueSpan, valueArray) => {
            this.shareValue = value;
            this.shareValuespan = valueSpan;
            setState({ browserRoleValue: valueArray});
        }}
        valueArray={browserRoleValue}
        />:""
         )
        )
      )
    }
    {
    (this.state.type=='267'?
     <Select
        label={getLabel('139','级别') }
        
        value={this.state.roleLevel}
        options={this.options2}
        onChange={(key, showname) => {
            this.setState({roleLevel:key})
        }}
      />:"")

    }
    { (this.state.type=='4'||this.state.type=='164')?
    <List>
       <List.Item className="item" extra={<Switch className="right" checked={this.state.en} onChange={this.changeLanguage} />}>
       {getLabel('125963','含下级') }
     </List.Item></List>:""

    }

     {this.state.type!='1'?
     <InputInterval
        label={getLabel('683','安全级别') }
        placeholder={getLabel('83869','请输入') }
        min={[0, 100]}
        max={[0, 100]}
        value={[10, 100]}
        required
        onChange={(v) => { 
            this.seclevel = v[0];
            this.secLevelMax = v[1];
        }}
      />:""}

        <TimeRangePicker
          mode="date"
          label={getLabel('509722','起止可查看时间') }
          placeholder={[getLabel('24978','开始日期') , getLabel('741','结束日期') ]}
          value={this.state.date}
          onChange={(v) => {
            this.canViewMinTime=v[0];
            this.canViewMaxTime=v[1];  
           }}
        />
         <Button style={{position:'absolute',width:'100%',bottom:'0'}} type="primary" onClick={(e)=>this.addShare(setState)} >{getLabel('30986','保存') }</Button><WhiteSpace />
      </div>
    );
  }
}